//
//  FourHomeViewController.swift
//  SwiftStudy
//
//  Created by chang on 2018/1/5.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit

class FourHomeViewController: UIViewController,UITableViewDelegate,UITableViewDataSource  {

    fileprivate var _tableView : UITableView!
    fileprivate var _dataArray : NSMutableArray!
    fileprivate let _firstRowHeigh : CGFloat = 160
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.defaultBackgroundColor
        
        //get app icon
        let infoPlist : NSDictionary = Bundle.main.infoDictionary! as NSDictionary
        let infoIcon : NSArray = infoPlist.value(forKeyPath: "CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles") as! NSArray
        let icon : String = infoIcon.lastObject as! String
        
        _dataArray = NSMutableArray.init(array: [["title":"第一个没有名字","image":icon,"vc":""],
                                                 ["title":"收藏","image":"me_collection","vc":""],
                                                 ["title":"图片选择","image":"me_PhotoAlbum","vc":"SelectPhotoViewController"],
                                                 ["title":"图片搜索","image":"me_PhotoSearch","vc":"SearchPhotoViewController"],
                                                 ["title":"说话表情","image":"me_faceEx","vc":"SpeakEmojiViewController"],
                                                 ["title":"清空数据","image":"me_clearOff","vc":""],
                                                 ["title":"关于我们","image":"me_aboutUs","vc":"AboutUsViewController"]])
        
        _tableView = UITableView.init(frame: CGRect.init(x:0, y:0, width:MAINSCREEN_WIDTH, height:self.view.height - self.tabBarHeight))
        _tableView.backgroundColor = UIColor.defaultBackgroundColor
        _tableView?.delegate = self
        _tableView?.dataSource = self
        _tableView.separatorStyle = .none
        self.view.addSubview(_tableView)
        
    }


    // MARK: UITableView's delegate & dataSource.
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return _dataArray.count
    }
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        var rowHeight : CGFloat = 0
        switch indexPath.row {
        case 0:
            rowHeight = _firstRowHeigh
            break
        case 1:
            rowHeight = 50
            break
        default:
            rowHeight = 50
            break
        }
        return rowHeight
    }
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "cell"
        
        let infoDic : NSDictionary = _dataArray.object(at: indexPath.row) as! NSDictionary
        let titleStr : String = infoDic.object(forKey: "title") as! String
        let headIcon : String = infoDic.object(forKey: "image") as! String
        
        var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
        if (cell==nil) {
            cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: identifier)
            cell?.selectionStyle = .none
            cell?.backgroundColor = _tableView.backgroundColor
            cell?.textLabel?.textColor = UIColor.defaultTextColor
            
            if (indexPath.row==0) {
                let headImage = UIImageView.init(frame: CGRect.init(x: (_tableView.width-100)/2, y: (_firstRowHeigh-100)/2, width: 100, height: 100))
                headImage.image = UIImage(named: headIcon)
                headImage.layer.cornerRadius = headImage.width/2
                headImage.layer.masksToBounds = true
                cell?.addSubview(headImage)
            }
            
            
            let lineView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: _tableView.bounds.width, height: 0.5))
            lineView.backgroundColor = UIColor.defaultLineColor
            lineView.tag = 10
            cell?.addSubview(lineView)
        }
        
        if (indexPath.row==0) {
            cell?.accessoryType = .none
            let lineView = cell?.viewWithTag(10)
            lineView?.top = _firstRowHeigh-0.5
        }else{
            cell?.accessoryType = .disclosureIndicator
            let lineView = cell?.viewWithTag(10)
            lineView?.top = 50-0.5
            
            cell?.textLabel?.text = titleStr
            cell?.imageView?.image = UIImage(named : headIcon)
        }
        
        return cell!
    }
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        let infoDic : NSDictionary = _dataArray.object(at: indexPath.row) as! NSDictionary
        let titleStr : String = infoDic.object(forKey: "title") as! String
        let vcStr : String = infoDic.object(forKey: "vc") as! String
        
        let vc = self.swiftClassFromString(className: vcStr)
        if vc != nil {
            vc?.title = titleStr
            vc?.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc!, animated: true)
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
